import React from 'react';
import {Button, Card, Form, Input, InputNumber, Space, Spin} from "antd";
import TextArea from "antd/es/input/TextArea";
import {Link} from "react-router-dom";
import {Part} from "type/Part";
import LogFactory from "log/LogFactory";
import GlobalHandler from "global/GlobalHandler";
import {MATHEMATICAL_PRECISION} from "constant/Common";

import {fetchAddPart} from "../../../redux/reducer/async/partAsyncAction";
import PartProviderSelect from "./PartProviderSelect";
import PartGroupSelect from "./PartGroupSelect";

const log = LogFactory.getLog("PartAddComponent");
const PartAddComponent = () => {
    const [form] = Form.useForm<Part>();
    const [loading, setLoading] = React.useState<boolean>(false);
    const initialValues: Partial<Part> = {
        order: 0,
        salePrice: 0.00,
        costPrice: 0.00,
        repertorySize: 0,
    };
    const onFinish = async (values: Part) => {
        setLoading(true);
        values.salePrice = values.salePrice * MATHEMATICAL_PRECISION;
        values.costPrice = values.costPrice * MATHEMATICAL_PRECISION;
        await GlobalHandler.getInstance().executeAddAsyncAction(fetchAddPart(values));
        setLoading(false);
    } ;

    return (
        <Card title={"添加零件信息"} extra={<Link to={".."}>零件列表</Link>}>
            <Spin spinning={loading}>
                <Form<Part>
                    layout={"horizontal"}
                    form={form}
                    labelCol={{span: 5}}
                    wrapperCol={{span: 10}}
                    initialValues={initialValues}
                    onFinish={onFinish}
                >
                    <Form.Item<Part> name={"partName"} label="零件名称"
                                     rules={[{required: true, message: '零件名称必须输入!'}]}>
                        <Input placeholder={"请输入零件名称"}/>
                    </Form.Item>
                    <Form.Item<Part> name={"figureNumber"} label="零件图号"
                                     rules={[{required: true, message: '零件图号必须输入!'}]}>
                        <Input placeholder={"请输入零件图号"}/>
                    </Form.Item>
                    <Form.Item<Part> name={"partDesc"} label="零件描述">
                        <Input placeholder={"请输入零件描述信息"}/>
                    </Form.Item>
                    <Form.Item<Part> name={"partSize"} label="零件尺寸">
                        <Input placeholder={"请输入零件尺寸"}/>
                    </Form.Item>
                    <PartProviderSelect form={form}/>
                    <PartGroupSelect form={form}/>
                    <Form.Item<Part> name={"repertorySize"} label="库存数量">
                        <InputNumber min={0} addonAfter={"个"}/>
                    </Form.Item>
                    <Form.Item<Part> name={"costPrice"} label="进货价格">
                        <InputNumber min={0.00} precision={2} addonAfter={"¥"}/>
                    </Form.Item>
                    <Form.Item<Part> name={"salePrice"} label="销售价格">
                        <InputNumber min={0.00} precision={2} addonAfter={"¥"}/>
                    </Form.Item>
                    <Form.Item<Part> name="order" label="数据排序">
                        <InputNumber min={0} addonAfter={"顺序"}/>
                    </Form.Item>
                    <Form.Item label="备注信息" name={"remark"}>
                        <TextArea rows={3} />
                    </Form.Item>
                    <Form.Item wrapperCol={{offset: 6,span: 16}}>
                        <Space>
                            <Button loading={loading} type="primary" htmlType="submit">添加</Button>
                            <Button type="link" htmlType="button">
                                填充字段
                            </Button>
                        </Space>
                    </Form.Item>
                </Form>
            </Spin>
        </Card>
    );
};

export default PartAddComponent;